/**
 * 
 */
package com.wolfpire.info.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wolfpire.info.dao.StaffCountInfoDao;
import com.wolfpire.info.dao.StaffDetailCountInfoDao;
import com.wolfpire.info.model.StaffCountInfo;
import com.wolfpire.info.model.StaffDetailCountInfo;
import com.wolfpire.info.service.StaffCountInfoService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;

/**
 * @author lihd
 *
 */
@Service("staffCountInfoService")
public class StaffCountInfoServiceImpl extends
		BaseHibernateService<StaffCountInfo, Long> implements
		StaffCountInfoService {

	@Override
	public List<StaffCountInfo> list(StaffCountInfo staffContInfo) {
		return this.staffContInfoDao.list(staffContInfo);
	}

	@Override
	public void del(Long id) {
		StaffCountInfo staffCountInfo = this.staffContInfoDao.get(id);
		if (null != staffCountInfo) {
			staffCountInfo.setDelFlag(Constants.DELETE_FLAG);
			this.staffContInfoDao.saveOrUpdate(staffCountInfo);
		}
	}
	
	@Override
	public StaffCountInfo getFullStaffCountInfo(Long id) {
		// TODO Auto-generated method stub
		StaffCountInfo staffCountInfo = this.staffContInfoDao.get(id);
		StaffDetailCountInfo filter = new StaffDetailCountInfo();
		filter.setStaffCountInfoId(id);
		List<StaffDetailCountInfo> details = this.staffDetailCountInfoDao.list(filter);
		for (StaffDetailCountInfo detail : details) {
			if ("school_level".equals(detail.getPostKey())) {
				//校级详情
				staffCountInfo.setSchoolLevelDetail(detail);
			} else if ("man_middle_level".equals(detail.getPostKey())) {
				//中层正职
				staffCountInfo.setManMiddleLevelDetail(detail);
			}  else if ("deputy_middle_level".equals(detail.getPostKey())) {
				//中层副职
				staffCountInfo.setDeputyMiddleLevelDetail(detail);
			}  else if ("general_manager".equals(detail.getPostKey())) {
				//一般管理人员
				staffCountInfo.setGeneralManagerDetail(detail);
			}  else if ("counselor".equals(detail.getPostKey())) {
				//专职辅导员
				staffCountInfo.setCounselorDetail(detail);
			}
		}
		return staffCountInfo;
	}

	@Override
	public Page<StaffCountInfo> setPageDataList(Page<StaffCountInfo> page,
			StaffCountInfo filterStaffContInfo) {
		return this.staffContInfoDao.findStaffContInfos(page, filterStaffContInfo);
	}

	@Override
	public void saveOrUpdate(StaffCountInfo staffCountInfo) {
		this.staffContInfoDao.saveOrUpdate(staffCountInfo);
		List<StaffDetailCountInfo> staffDetailCountInfos = new ArrayList<StaffDetailCountInfo>();
		staffDetailCountInfos.add(staffCountInfo.getSchoolLevelDetail());
		staffDetailCountInfos.add(staffCountInfo.getManMiddleLevelDetail());
		staffDetailCountInfos.add(staffCountInfo.getDeputyMiddleLevelDetail());
		staffDetailCountInfos.add(staffCountInfo.getGeneralManagerDetail());
		staffDetailCountInfos.add(staffCountInfo.getCounselorDetail());
		// 删除子表关联记录，然后重新插入
		Long id = staffCountInfo.getId();
		this.staffDetailCountInfoDao.deleteByStaffCountInfoId(id);
		for (StaffDetailCountInfo detail : staffDetailCountInfos) {
			detail.setStaffCountInfoId(id);
		}
		this.staffDetailCountInfoDao.batchSave(staffDetailCountInfos);
	}


	@Override
	protected IBaseHibernateDao<StaffCountInfo, Long> getEntityDao() {
		return this.staffContInfoDao;
	}
	
	@Resource private StaffCountInfoDao staffContInfoDao;
	
	@Resource private StaffDetailCountInfoDao staffDetailCountInfoDao;

}
